package com.ekuaitu.kuaitustaff.baiduMap;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.Polyline;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.ekuaitu.kuaitustaff.application.MyApplication;

import java.util.ArrayList;
import java.util.List;


//public class MyPoiOverlay extends PoiOverlay {
//private PoiSearch mPoiSearch;
//    public MyPoiOverlay(BaiduMap baiduMap,PoiSearch mPoiSearch) {
//        super(baiduMap);
//        this.mPoiSearch=mPoiSearch;
//    }
//
//
//
//    public boolean onPoiClick(int index) {
//        super.onPoiClick(index);
//        PoiInfo poi = getPoiResult().getAllPoi().get(index);
//        // if (poi.hasCaterDetails) {
//        mPoiSearch.searchPoiDetail((new PoiDetailSearchOption())
//                .poiUid(poi.uid));
//        return true;
//    }
//}

/**
 * 覆盖物
 */

public class MyPoiOverlay extends OverlayManager {
    private PoiResult poiResult = null;
    private PoiSearch mPoiSearch;
    private int bitmapResource;

    public MyPoiOverlay(BaiduMap baiduMap, PoiSearch mPoiSearch, int bitmapResource) {
        super(baiduMap);
        this.mPoiSearch = mPoiSearch;
        this.bitmapResource = bitmapResource;
    }

    public void setData(PoiResult poiResult) {
        this.poiResult = poiResult;
    }

    @Override
    public boolean onMarkerClick(Marker marker) {
      /*  if (marker.getExtraInfo() != null) {
            int index = marker.getExtraInfo().getInt("index");
            PoiInfo poi = poiResult.getAllPoi().get(index);

            // 详情搜索
            mPoiSearch.searchPoiDetail((new PoiDetailSearchOption())
                    .poiUid(poi.uid));
            Log.d("Log","name====="+poi.name+"");
            return true;
        }
        return false;*/
        return false;
    }

    @Override
    public List<OverlayOptions> getOverlayOptions() {
        if ((this.poiResult == null) || (this.poiResult.getAllPoi() == null))
            return null;
        ArrayList<OverlayOptions> arrayList = new ArrayList<OverlayOptions>();
        for (int i = 1; i < this.poiResult.getAllPoi().size(); i++) {
            if (this.poiResult.getAllPoi().get(i).location == null)
                continue;
            // 给marker加上标签
            Bundle bundle = new Bundle();
            bundle.putInt("index", i);
            arrayList.add(new MarkerOptions()
                    .icon(BitmapDescriptorFactory
                            .fromBitmap(setNumToIcon(i))).extraInfo(bundle)
                    .position(this.poiResult.getAllPoi().get(i).location));

        }
        return arrayList;
    }


    /**
     * 往图片添加数字
     */

    private Bitmap setNumToIcon(int num) {
        BitmapDrawable bd = (BitmapDrawable) MyApplication.getInstance().getResources().getDrawable(
                bitmapResource);
        Bitmap bitmap = bd.getBitmap().copy(Bitmap.Config.ARGB_8888, true);
        Canvas canvas = new Canvas(bitmap);

        Paint paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setAntiAlias(true);
        int widthX;
        int heightY = 0;
        if (num < 10) {
            paint.setTextSize(30);
            widthX = 8;
            heightY = 6;
        } else {
            paint.setTextSize(20);
            widthX = 11;
        }

       /* canvas.drawText(String.valueOf(num),
                ((bitmap.getWidth() / 2) - widthX),
                ((bitmap.getHeight() / 2) + heightY), paint);*/
        return bitmap;
    }

    @Override
    public boolean onPolylineClick(Polyline polyline) {
        return false;
    }
}


